package com.Order;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Timestamp;
import java.util.Date;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.Result.BaseRespones;
import com.Result.StatusCode;
import com.Washer.Dormitory;
import com.Washer.DormitoryMapper;
import com.Washer.Washer;
import com.Washer.WasherMapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class SubmitState {
	/**
	 * @apiDefine Order 订单
	 */
	
	/**
	 * @apiDefine submitOrderErrorMsg 订单提交错误响应信息
	 * @apiError 5013 更新订单表失败
	 */
	
	/**
	 * @apiDefine submitOrderSuccessMsg 订单提交成功响应信息
	 * @apiSuccess {int} code 响应码
	 * @apiSuccess {String} msg 响应信息
	 * @apiSuccess {timestamp} timestamp 时间戳
	 * @apiSuccess {Object} res 订单对象
	 */
	
	/**
	 * @api{Insert} /Order?method=submit 用户提交订单
	 * @apiName submit
	 * @apiGroup Order
	 * @apiDescription 用户提交订单 
	 * @apiVersion 0.0.1
	 * 
	 * @apiParamExample {json} 提交订单请求样例
	 * { 
	 *	"StuID":10004,
	 *	"DormID":10002,
	 *	"WasherID":10006,
	 *	"OrderMoney":5,
	 *	"OrderPattern":1
	 *	}
	 * 
	 * @apiUse submitOrderErrorMsg
	 * @apiUse submitOrderSuccessMsg
	 * 
	 * @apiSuccessExample {json} 订单提交成功
	 * {
	 * 'code':5010,
	 * 'msg':'提交订单成功',
	 * 'timestamp':'2019-06-08 22:18:34',
	 * 'data':
	 * {
	 * 'OrderID':10006,
	 * 'DormID':10002,
	 * 'WasherID':10006,
	 * 'StuID':10004,
	 * 'OrderStatus':1,
	 * 'OrderMoney':5,
	 * 'OrderPattern':1,
	 * 'VerCode':'0529',
	 * 'CreateTime':'2019-06-08 22:18:33'
	 * }
	 * }
	 * 
	 */
	public String submitNewOrders(String requestString) throws IOException {
		String resource = "mybatisConfig.xml";
		InputStream is = org.apache.ibatis.io.Resources.getResourceAsStream(resource);
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
		SqlSession session = sessionFactory.openSession();
		// 返回对象声明
		BaseRespones response = null;
		OrderMapper orderMapper=(OrderMapper) session.getMapper(OrderMapper.class);
		WasherMapper washerMapper=(WasherMapper) session.getMapper(WasherMapper.class);
		DormitoryMapper dormitoryMapper=(DormitoryMapper) session.getMapper(DormitoryMapper.class);
		
		
		System.out.println(requestString);
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		
		// 获取提交的订单类
		Order submit=null;
		try {
			submit=gson.fromJson(requestString, Order.class);
		} catch (Exception e) {
			response = new BaseRespones(StatusCode.jsonDeserializeFail);
			return gson.toJson(response);
		}
		if (submit==null) {
			response = new BaseRespones(StatusCode.jsonDeserializeFail);
			return gson.toJson(response);
		}
		// 订单为提交状态
		submit.setOrderStatus(1);
		// 时间
		submit.setCreateTime(new Timestamp(new Date().getTime()));
		//验证码
		submit.setVerCode("0529");
		
		Dormitory dormitory=new Dormitory();
		dormitory.setDormID(submit.getDormID());
		
		Washer washer=new Washer();
		washer.setDormID(submit.getDormID());
		washer.setWasherID(submit.getWasherID());
		
		System.out.println(submit);
		int rs=orderMapper.insertNewOrder(submit);
		// 写入订单表失败
		if (rs==0) {
			session.rollback();
			response=new BaseRespones(StatusCode.submitOrderFailOrder);
			return gson.toJson(response);
		}
		else {
			session.commit();
			rs=dormitoryMapper.reduceFreeNumber(dormitory);
			session.commit();
			rs=washerMapper.changeStatus(washer);
			session.commit();
			response=new BaseRespones(StatusCode.submitOrderSuccess);
			response.setData(submit);
			return gson.toJson(response);
		}
	}

}
